#include <string>

#include <log4cxx/logger.h>
#include <json/json.h>

#include "NameSvrProtoHelper.h"

using std::string;

using namespace log4cxx;

static LoggerPtr g_logger = Logger::getLogger("NameSvrProtoHelper");

string ns_build_get_file_metadata_info_response(int num_hard_link, int file_type, int mds_id, int dir_id, int64_t file_id) {
	json_object * j = json_object_new_object();
	json_object * j_result = json_object_new_int(0);
	json_object * j_num_hard_link = json_object_new_int(num_hard_link);
	json_object * j_file_type = json_object_new_int(file_type);
	json_object * j_mds_id = json_object_new_int(mds_id);
	json_object * j_dir_id = json_object_new_int(dir_id);
	json_object * j_file_id = json_object_new_int(file_id);
	json_object_object_add(j, "RESULT", j_result);
	json_object_object_add(j, "NUM_HARD_LINK", j_num_hard_link);
	json_object_object_add(j, "FILE_TYPE", j_file_type);
	json_object_object_add(j, "MDS_ID", j_mds_id);
	json_object_object_add(j, "DIR_ID", j_dir_id);
	json_object_object_add(j, "FILE_ID", j_file_id);
	string response = json_object_to_json_string(j);
	json_object_put(j);
	return response;
}

string ns_build_alloc_mds_4_new_file_response(int32_t mds_id) {
	json_object * j = json_object_new_object();
	json_object * j_result = json_object_new_int(0);
	json_object * j_mds_id = json_object_new_int(mds_id);
	json_object_object_add(j, "RESULT", j_result);
	json_object_object_add(j, "MDS_ID", j_mds_id);
	string response = json_object_to_json_string(j);
	json_object_put(j);
	return response;
}

string ns_build_query_is_file_exist_response(int exist_flag) {
	json_object * j = json_object_new_object();
	json_object * j_result = json_object_new_int(0);
	json_object * j_exist_flag = json_object_new_int(exist_flag);
	json_object_object_add(j, "RESULT", j_result);
	json_object_object_add(j, "EXIST_FLAG", j_exist_flag);
	string response = json_object_to_json_string(j);
	json_object_put(j);
	return response;
}

string ns_build_query_is_dir_exist_response(int exist_flag) {
	json_object * j = json_object_new_object();
	json_object * j_result = json_object_new_int(0);
	json_object * j_exist_flag = json_object_new_int(exist_flag);
	json_object_object_add(j, "RESULT", j_result);
	json_object_object_add(j, "EXIST_FLAG", j_exist_flag);
	string response = json_object_to_json_string(j);
	json_object_put(j);
	return response;
}

string ns_build_read_dir_response(list<string> entries) {
	json_object * j = json_object_new_object();
	json_object * j_result = json_object_new_int(0);
	json_object * j_entries = json_object_new_array();
	for(list<string>::const_iterator iter = entries.begin(); entries.end() != iter; iter++) {
		json_object * j_entry = json_object_new_string(iter->c_str());
		json_object_array_add(j_entries, j_entry);
	}
	json_object_object_add(j, "RESULT", j_result);
	json_object_object_add(j, "ENTRIES", j_entries);
	string response = json_object_to_json_string(j);
	json_object_put(j);
	return response;
}
